/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package helper;

import com.sun.lwuit.Component;
import com.sun.lwuit.Container;
import com.sun.lwuit.Font;
import com.sun.lwuit.Image;
import com.sun.lwuit.Label;
import com.sun.lwuit.List;
import com.sun.lwuit.layouts.BorderLayout;
import com.sun.lwuit.layouts.BoxLayout;
import com.sun.lwuit.list.ListCellRenderer;
import java.io.IOException;


import model.ItemMenu;

/**
 *
 * @author Hallucinogen5
 */
public class ListRenderer extends Container implements ListCellRenderer {
    //Variable

    private Label name = new Label("");
    private Label focus = new Label("");
    private Label pic = new Label("");
    private Image picImg;
    public static float gapBetween = 12.5f;

    //Default constructor
    public ListRenderer() {

        System.out.println("ListRenderer()");
        Image img = null;
        String _URL = null;
        try {
            _URL = Util.getInstance().getBackgroung("tittle");
            img = Image.createImage(MobileDownloader.download(_URL), 0, MobileDownloader.download(_URL).length);
        } catch (Exception e) {
            try {
                img = Image.createImage("/res/image/green_title.png");
            } catch (IOException ex) {
                System.out.println("Background image not found");
            }
        }

        setLayout(new BorderLayout());
        addComponent(BorderLayout.WEST, pic);
        Container cnt = new Container(new BoxLayout(BoxLayout.Y_AXIS));
        name.getStyle().setBgTransparency(0);
        name.getStyle().setFont(Font.createSystemFont(Font.FACE_PROPORTIONAL, Font.STYLE_BOLD, Font.SIZE_SMALL));
        cnt.addComponent(name);
        addComponent(BorderLayout.CENTER, cnt);

//        focus.getStyle().setBgTransparency(100);
        focus.getStyle().setBgImage(img);
    }

    //User-defined constructor
    public ListRenderer(String mode) {
        setLayout(new BorderLayout());
        addComponent(BorderLayout.WEST, name);

        focus.getStyle().setBgColor(0xFFFF00);
        focus.getStyle().setFgColor(0xFFFFFF);
    }

    //User-defined constructor
    public ListRenderer(Image focusImg) {
        setLayout(new BorderLayout());
        addComponent(BorderLayout.CENTER, name);

        focus.getStyle().setBgTransparency(0);
        focus.setIcon(focusImg);

    }

    public Component getListCellRendererComponent(List list, Object o, int i, boolean isSelected) {
        if (o instanceof Image) {
            name.setIcon((Image) o);
            name.setAlignment(Component.LEFT);
            if (this.picImg != null) {
                name.getStyle().setMargin(0, 0, picImg.getWidth(), 0);
            }
            name.getStyle().setBgTransparency(0);
        } else if (o instanceof String) { //String
            name.setText(o.toString());

            name.getStyle().setBgTransparency(0);
            name.getStyle().setFgColor(0x000000);
        } else if (o instanceof ItemMenu) { //Item data

            ItemMenu menu = (ItemMenu) o;
            name.setText(menu.getMenuName());

            pic.setIcon(menu.getIcon());

//            System.out.println("name item menu"+menu.getMenuName());
            //content.sets`
//            Font font = Font.createSystemFont(Font.FACE_SYSTEM, Font.STYLE_BOLD, Font.SIZE_MEDIUM);
//            name.getStyle().setFont(font);
//            //No background
//            name.getStyle().setBgTransparency(0);
//            name.getStyle().setFgColor(0x000000);


        }
        return this;
    }

    public Component getListFocusComponent(List list) {
        return focus;
    }
}
